//
//  ShopGoodsDetailVC.m
//  ForestPack
//
//  Created by jimmy on 2018/7/30.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ShopGoodsDetailVC.h"
#import "SimpleHeaderView.h"
#import "ShopGoodsDetailCell.h"
#import "ShopGoodsDetailModel.h"
#import "KDCycleBannerView.h"//轮播图

#import "PublushGoodsViewController.h"

@interface ShopGoodsDetailVC ()<UITableViewDelegate,UITableViewDataSource, KDCycleBannerViewDataSource, KDCycleBannerViewDelegate>

@property (nonatomic,strong)  BaseTableView         * tableView;
@property (nonatomic,strong)  NSArray               * mainPicsArray;
@property (nonatomic,strong)  NSArray               * detailPicsArray;
@property (nonatomic,strong)  ShopGoodsDetailModel  * shopGoodsDetailModel;
@property (nonatomic, strong) KDCycleBannerView     * cycleBannerViewBottom;//轮播图

@end

@implementation ShopGoodsDetailVC

- (NSArray *)mainPicsArray
{
    if (_mainPicsArray == nil) {
        _mainPicsArray = [[NSArray alloc] init];
    }
    return _mainPicsArray;
}

- (NSArray *)detailPicsArray
{
    if (_detailPicsArray == nil) {
        _detailPicsArray = [[NSArray alloc] init];
    }
    return _detailPicsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];


    [self setTitle:@"商品详情"];
    [self setNavUI];
    [self setTableViewUI];

    [self refreshData];
    [self requestData];
}

-(void)refreshData
{
    /** 为了避免 block 的循环引用导致内存泄漏  要使用__weakSelf 来代替self */
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)requestData
{
    WeakObj(self);
    NSString *url = [NSString stringWithFormat:@"%@app/shop_product_detail",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken],@"goods_id":self.goods_id};
    [HttpManager showWait];
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        [HttpManager hideWait];
        [selfWeak.tableView.mj_header endRefreshing];
        NSDictionary * dict = successResponse;
        if (dict == nil) {
            return ;
        }
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]){
            [HttpManager hideWait];
            NSError *error;
            ShopGoodsDetailModel * model= [[ShopGoodsDetailModel alloc] initWithDictionary:dict[@"data"] error:&error];
            selfWeak.shopGoodsDetailModel = model;
            selfWeak.mainPicsArray = model.mian_pics;
            selfWeak.detailPicsArray = model.detail_pics;
            [selfWeak.cycleBannerViewBottom reloadDataWithCompleteBlock:nil];
            [selfWeak.tableView reloadData];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
        
    }];    
}

- (void)upClick
{
    
}

- (void)downClick
{
    
}

-(void)changeClick
{
    PublushGoodsViewController *vc = [[PublushGoodsViewController alloc] init];
    vc.shopGoodsDetailModel = _shopGoodsDetailModel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setNavUI
{
    UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [editBtn setTitle:@"修改" forState:UIControlStateNormal];
    [editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    editBtn.titleLabel.font = Kfont(14);
    editBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
    [editBtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
    
    UIButton *upBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [upBtn setTitle:@"上架" forState:UIControlStateNormal];
    [upBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    upBtn.titleLabel.font = Kfont(14);
    upBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
    [upBtn addTarget:self action:@selector(upClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *upItem = [[UIBarButtonItem alloc] initWithCustomView:upBtn];
    
    UIButton *downBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [downBtn setTitle:@"下架" forState:UIControlStateNormal];
    [downBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    downBtn.titleLabel.font = Kfont(14);
    downBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
    [downBtn addTarget:self action:@selector(downClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *downItem = [[UIBarButtonItem alloc] initWithCustomView:downBtn];
    
    if (_isEdit) {
        if ([_statu integerValue] == 0) {
            self.navigationItem.rightBarButtonItems = @[editItem,downItem];
        }else if ([_statu integerValue] == 3) {
            self.navigationItem.rightBarButtonItems = @[editItem,upItem];
        }else {
            self.navigationItem.rightBarButtonItem = editItem;
        }
    }
}

- (void)setTableViewUI
{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT)style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.cycleBannerViewBottom = [KDCycleBannerView new];
    self.cycleBannerViewBottom.frame = CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(180)); //位置及宽高
    self.cycleBannerViewBottom.datasource = self;
    self.cycleBannerViewBottom.delegate = self;
    self.cycleBannerViewBottom.continuous = YES; //是否连续显示
    self.cycleBannerViewBottom.autoPlayTimeInterval = 4; //时间间隔
    self.tableView.tableHeaderView = self.cycleBannerViewBottom;
    
    [self.view addSubview:self.tableView];
  
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.detailPicsArray.count;//返回的应该是店铺产品详情detail_pics产品详情图数组的个数
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 140;
    }else{
        return 180;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        SimpleHeaderView *headerView = [SimpleHeaderView headerViewWithTitle:@"商品信息：" withHeight:44];
        [view addSubview:headerView];
        return view;
    }else{
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        SimpleHeaderView *headerView = [SimpleHeaderView headerViewWithTitle:@"商品详情：" withHeight:44];
        [view addSubview:headerView];
        return view;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        ShopGoodsDetailCell *cell = [ShopGoodsDetailCell cellWithTableView:tableView andIndexPath:indexPath];
        if (self.shopGoodsDetailModel) {
            cell.shopGoodsDetailModel = self.shopGoodsDetailModel;
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsDetailCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsDetailCell"];
        }
        for (UIView *subView in cell.subviews) {
            [subView removeFromSuperview];
        }
        UIImageView *detailImg = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 160)];
        ShopGoodsDetailPicsModel * model = _detailPicsArray[indexPath.row];
        [detailImg sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil options:SDWebImageRetryFailed];
        [cell addSubview:detailImg];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark 轮播图代理委托

- (NSArray *)numberOfKDCycleBannerView:(KDCycleBannerView *)bannerView{
    NSMutableArray *urlArray = [NSMutableArray array];
    for (int i = 0; i < self.mainPicsArray.count; i++) {
        ShopGoodsMainPicsModel *model = self.mainPicsArray[i];
        NSString *url = [NSString stringWithFormat:@"%@",model.pic];
        [urlArray addObject:url];
    }
    return urlArray;
}

- (UIViewContentMode)contentModeForImageIndex:(NSUInteger)index{
    return UIViewContentModeScaleToFill;
}

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didSelectedAtIndex:(NSUInteger)index{
    NSLog(@"点击了第%ld个",(long)index);
}

@end
